<template>
<div class="app-container">
  <div style="width: 800px">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="商品名称" prop="name">
        <el-input clearable size="small" v-model="form.name" placeholder="请输入商品名称" />
      </el-form-item>
      <el-form-item label="缩略图" required>
        <div style="color:#999">
          建议尺寸750*560px或4：3，JPG、PNG格式， 图片小于5M。
        </div>
        <el-upload
          :class="imgFilesList.length?'hiddenUpload':''"
          action="https://service.bairicun.com/file/upload"
          list-type="picture-card"
          :file-list="imgFilesList"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="积分金额" prop="cost">
        <el-input clearable size="small" v-model="form.cost" placeholder="请输入积分金额" />
      </el-form-item>
      <el-form-item label="商品数量" prop="quantity">
        <el-input clearable size="small" v-model="form.quantity" placeholder="请输入商品数量" />
      </el-form-item>
      <el-form-item label="展示排序" prop="sequencing">
        <el-input clearable size="small" v-model="form.sequencing" placeholder="请输入展示排序" />
      </el-form-item>
      <el-form-item label="商品描述" prop="description">
        <el-input clearable placeholder="请输入商品描述" type="textarea" :rows="5" size="small" v-model="form.description" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit('form')">保存</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>

import {
  saveGoodsApi,
  getGoodsDetailApi
} from '@/api/repurchase'
export default {
  data() {
    return {
      form: {
        name: '',
        cost: '',
        quantity: '',
        sequencing: '',
        description: '',
      },
      rules: {
        name: [{
          required: true,
          message: '请输入商品标题',
          trigger: 'blur'
        }, ],
        cost: [{
          required: true,
          message: '请输入积分金额',
          trigger: 'blur'
        }, ],
        quantity: [{
          required: true,
          message: '请输入商品数量',
          trigger: 'blur'
        }, ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadStatus: false,
      uploadUrl: '',
      imgFilesList: [],
      typeList: []
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    changeClassify(e) {
      this.form.classify = e
    },
    getDetail() {
      getGoodsDetailApi(this.$route.query.editId).then((res) => {
        this.form.name = res.data.name
        this.form.cost = res.data.cost
        this.form.quantity = res.data.quantity
        this.form.sequencing = res.data.sequencing
        this.form.description = res.data.description
        this.imgFilesList = [
          {
            url: res.data.imageUrl,
            id: res.data.image,
          }
        ]
      })
    },
    changeLink(e) {
      if(e) {
        this.rules.content[0].required = false
      } else {
        this.rules.content[0].required = true
      }
    },
    handleRemove(file, fileList) {
      this.uploadUrl = ''
      this.imgFilesList = []
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(e) {
      if(e.code == 200) {
        this.uploadStatus = true
        this.uploadUrl = e.data.url
        this.imgFilesList = [
          {
            url: e.data.url,
            id: e.data.id,
          }
        ]
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.imgFilesList.length) {
            saveGoodsApi({
              id: this.$route.query.editId,
              name: this.form.name,
              cost: this.form.cost,
              quantity: this.form.quantity,
              sequencing: this.form.sequencing,
              description: this.form.description,
              image: this.imgFilesList[0].id,
              thumb: this.imgFilesList[0].id,
            }).then((res) => {
              this.$message.success('操作成功！');
              setTimeout(() => {
                this.$router.back(-1)
              }, 1000);
            })
          } else {
            this.$message.error('请上传缩略图！');
          }
        } else {
          return false;
        }
      });
    },
    onCancel() {
      this.$router.back(-1)
    }
  }
}
</script>
<style>
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>
<style scoped>
.line {
  text-align: center;
}
</style>
